import base from 'core/config/base/base'
import vip from 'core/config/base/vip'
import partner from 'core/config/base/partner'
import monster from 'core/config/base/monster'
import map from 'core/config/base/map'
import luck from 'core/config/base/luck'
import power from 'core/config/base/power'
import battle from 'core/config/base/battle'
import need from './base/need'
import gem from './base/gem'
import attriLvlup from './base/attriLvlup'
import equipment from './base/equipment'
import skill from './base/skill'
import market from './base/market'
import talent from './base/talent'
import treasure from './base/treasure'
import research from './base/research'
import task from './base/task'
import arena from './base/arena'
import pet from './base/pet'
import adventure from './base/adventure'
// import magicMachine from './base/magicMachine'
import altar from './base/altar'
import recruit from './base/recruit'
import fate from './base/fate'
import item from './base/item'
import friendship from './base/friendship'
import spirit from './base/spirit'


import items_200001 from './items/items_200001'
import items_201001 from './items/items_201001'
import items_202001 from './items/items_202001'
import items_203001 from './items/items_203001'
import items_204001 from './items/items_204001'
import items_205001 from './items/items_205001'
import items_206001 from './items/items_206001'
import items_207001 from './items/items_207001';
import items_209001 from './items/items_209001';
import monsters_310001 from './monsters/monsters_310001'
import monsters_310002 from './monsters/monsters_310002'
import monsters_310003 from './monsters/monsters_310003'
import partners_320001 from './partners/partners_320001'
import maps_300001 from './maps/maps_300001'
import maps_300002 from './maps/maps_300002'
import maps_300003 from './maps/maps_300003'
import skills_270001 from './skills/skills_270001'
import skills_270002 from './skills/skills_270002'
import skills_270003 from './skills/skills_270003'
import skills_270004 from './skills/skills_270004'
import skills_270005 from './skills/skills_270005'
import buffs_280001 from './buffs/buffs_280001'
import buffs_280002 from './buffs/buffs_280002'
import treasure_440001 from './treasure/treasure_440001';
import immune_450001 from './immune/immune_450001';
import task_210001 from './task/task_210001';
import research_460001 from './research/research_460001';
import magicMachine_470001 from './magicMachine/magicMachine_470001';
import packageAttributes_480001 from './packageAttributes/packageAttributes_480001';
import friendship_490001 from './friendship/friendship_490001';
import spirit_510001 from './spirit/spirit_510001';

import { nbase64, nbase642 } from 'core/utils/newEnco'
import utils from 'core/utils/utils'
import formation from './base/formation'
import formation_500001 from './formation/formation_500001'
import items_208001 from './items/items_208001'
import halidom from './base/halidom'

import { configByIDCopy } from './configByIDData'
import { gameConfigCopy } from './gameConfigData'
import areaCs from './base/areaCs'

export let propName: any = {}
export let configByID: any = {}

function getTemplateByTID(templateInfos: any[]) {
    let data = {}
    for (let j = 0; j < templateInfos.length; j++) {
        let templateInfo = templateInfos[j]
        for (let i in templateInfo) {
            data[i] = templateInfo[i]
            let id = parseInt(i)
            data[i]['tid'] = id
            data[i]['type'] = Math.floor(id / 10000)
            data[i]['subType'] = Math.floor(id % 10000 / 100)
            if (templateInfo[i].name) {
                propName[i] = templateInfo[i].name
            }
            configByID[id] = data[i]
        }
    }
    return data
}

export let gameConfig = {
    base,
    need,
    vip,
    partner,
    monster,
    map,
    luck,
    power,
    battle,
    gem,
    equipment,
    attriLvlup,
    skill,
    market,
    talent,
    treasure,
    research,
    task,
    adventure,
    arena,
    pet,
    magicMachine: {},
    altar,
    recruit,
    fate,
    item,
    formation,
    halidom,
    friendship,
    spirit,

    areaCs,
    maps: getTemplateByTID([maps_300001, maps_300002, maps_300003]),
    partners: getTemplateByTID([partners_320001]),
    monsters: getTemplateByTID([monsters_310001, monsters_310002, monsters_310003]),
    skills: getTemplateByTID([skills_270001, skills_270002, skills_270003, skills_270004, skills_270005]),
    buffs: getTemplateByTID([buffs_280001, buffs_280002]),
    items: getTemplateByTID([items_200001, items_201001, items_202001, items_203001, items_205001, items_206001, items_207001, items_208001, items_209001]),
    equipments: getTemplateByTID([]),
    treasures: getTemplateByTID([treasure_440001]),
    immunes: getTemplateByTID([immune_450001]),
    tasks: getTemplateByTID([task_210001]),
    researchs: getTemplateByTID([research_460001]),
    packageAttributes: getTemplateByTID([packageAttributes_480001]),
    friendships: getTemplateByTID([friendship_490001]),
    spirits: getTemplateByTID([spirit_510001]),
    // magicMachines: getTemplateByTID([magicMachine_470001]),
    formations: getTemplateByTID([formation_500001]),
}

for (let key in gameConfig.item) {
    if (gameConfig.item.hasOwnProperty(key)) { // 检查是否为对象的自身属性
        let value = gameConfig.item[key]
        propName[key] = value.name
    }
}

function createGameFile() {
    let configByIDCopy = JSON.parse(JSON.stringify(configByID))
    utils.entries(configByIDCopy).map(item => {
        let data = item[1]
        if (data.desc) {
            delete data.desc
        }
        if (data.name) {
            delete data.name
        }
    })
    let gameConfigCopy = JSON.parse(JSON.stringify(gameConfig))
    utils.values(gameConfigCopy).map(itemData => {
        utils.values(itemData).map(item => {
            if (item.desc) {
                delete item.desc
            }
            if (item.name) {
                delete item.name
            }
        })
    })
    let gameConfigData = nbase642.encrypt(JSON.stringify(gameConfigCopy), 'qihuan123789')
    let configByIDData = nbase642.encrypt(JSON.stringify(configByIDCopy), 'qihuan123789')
    return { configByIDData, gameConfigData }
}

class GameConfigHandle {
    gameConfig = gameConfig
    propName = propName
    configByID = configByID
    createGameFile: any = createGameFile
    configByIDStr: string
    gameConfigStr: string
    constructor() {
        this.configByIDStr = JSON.stringify(configByID)
        this.gameConfigStr = JSON.stringify(gameConfig)
        // let configByIDStr = configByIDCopy
        // let gameConfigStr = gameConfigCopy
        // this.configByIDStr = nbase642.decrypt(configByIDStr, 'qihuan123789')
        // this.gameConfigStr = nbase642.decrypt(gameConfigStr, 'qihuan123789')
    }
    setConfig() {
        let data = JSON.parse(this.configByIDStr)
        this.configByID = data
        data = JSON.parse(this.gameConfigStr)
        this.gameConfig = data
    }
}


export let gameConfigHandle = new GameConfigHandle()